package com.zjhcsoft.common.action;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
public abstract class SysAbstractAction extends ActionSupport {
	private static final long serialVersionUID = -3603200651442111330L;

	//消息
	private String message;

	//文件路径
	private String filePath;


	//是否是为页面返回按钮，这样去掉可能带来的查询条件rongrong.ding
	private String back;

	//AJAX请求所带的时间参数rongrong.ding
	private String nowTime;


	/**
	 * 
	 * 导出EXCEL，已知道DATALIST的情况下调用
	 * @param o
	 * @param fileName
	 * @param items
	 * @throws ClassNotFoundException
	 */
//	protected void exportExcel(Class type,
//							   String fileName, 
//							   String[] items) throws ClassNotFoundException{
//		ExcelHelper.exportExcel(this.getDataList(), type, fileName, items);
//	}

	/**
	 * rongrong.ding
	 * LIST是从其他地方调过来的话调用此方法,导出EXCEL文件
	 * @param dataList
	 * @param o
	 * @param fileName
	 * @param items
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
//	protected void exportExcel(List dataList,Class type,
//			String fileName, String[] items) throws ClassNotFoundException{
//		this.setDataList(dataList);
//		exportExcel(type,fileName,items);
//	}
	
    /**
     *
     * importExcel:(读取EXCEL数据(第一sheet页)，放入指定对象)
     * @param  @param o
     * @param  @param filePath
     * @param  @param items    设定文件
     * @return void    DOM对象
     * @throws
     * @since  CodingExample　Ver 1.1
     */
//	protected void importExcel(Class o,String filePath,String[] items){
//		this.setDataList(ExcelHelper.readExcel(filePath, o,items,1));
//	}
//	
//	protected List readExcel(Class o,String filePath,String[] items){
//		return ExcelHelper.readExcel(filePath, o,items,1);
//	}

	/*
	 * 得到系统的根目录
	 */
	public String getContextRoot() {
		return this.getRequest().getContextPath();
	}

	/**
	 * 获得当前用户request对象
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得当前用户Response对象
	 * @return
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

    /**
     * 获得当前用户的session
     * @return
     */
	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 获得用户SESSION
	 */
	public Object getUserSession(){
		Object userob=getSession().getAttribute("userSession");
		if(userob!=null)
			return userob;
		else
			return null;
	}
	

	/*
	 * 设置session
	 */
	public void setSession(String key, Object object) {
		ServletActionContext.getRequest().getSession()
				.setAttribute(key, object);
	}
	/*
	 * 设置session
	 */
	public Object getSession(String key) {
		return ServletActionContext.getRequest().getSession().getAttribute(key);
	}
	/*
	 * 清空session
	 */
	public void removeSession(String sessionname) {
		if (this.getSession().getAttribute(sessionname) != null) {
			this.getSession().removeAttribute(sessionname);
		}
	}


	/*-------------分页专用----------------*/
	/**
	 * 记录总数
	 */
	private long total;
	/**
	 * 一页显示数目
	 */
	private long pagesize = 15;
	/**
	 * 总的页数
	 */
	private long pagetotal;
	/**
	 * 当前显示页
	 */
	private long page;

	private long nextpage;
	private long previouspage;
	private long lastpage;
	private long firstrownum;
	private long lastrownum;

	/*-----hibernate分页专用-----*/

	private static String setfirstrownummethod="setFirstResult";
	private static String setmaxrownummethod="setMaxResults";
	private static String listmethod="list";


	/**
	 * 数据列表
	 */
	@SuppressWarnings("unchecked")
	private List dataList;

	/**
	 * @return Returns the dateList.
	 */
	@SuppressWarnings("unchecked")
	public List getDataList() {
		return dataList;
	}

	/**
	 * @return Returns the currentPage.
	 */
	public long getPage() {
		return page;
	}

	/**
	 * @param currentPage
	 *            The currentPage to set.
	 */
	public void setPage(long page) {
		this.page = page;
	}

	/**
	 * @return Returns the pageSize.
	 */
	public long getPagesize() {
		return pagesize;
	}

	/**
	 * @param pageSize
	 *            The pageSize to set.
	 */
	public void setPagesize(long pageSize) {
		this.pagesize = pageSize;
	}

	/**
	 * @return Returns the totalNum.
	 */
	public long getTotal() {
		return total;
	}

	/**
	 * @return Returns the totalPage.
	 */
	public long getPagetotal() {
		return pagetotal;
	}

	/**
	 * @return Returns the lastpage.
	 */
	public long getLastpage() {
		this.lastpage = this.pagetotal;
		return lastpage;
	}

	/**
	 * @return Returns the nextpage.
	 */
	public long getNextpage() {
		if (this.page > this.pagetotal) {
			this.nextpage = this.lastpage;
		} else {
			this.nextpage = this.page + 1;
		}
		return nextpage;
	}

	/**
	 * @return Returns the previouspage.
	 */
	public long getPreviouspage() {
		if (this.page == 1) {
			this.previouspage = 1;
		} else {
			this.previouspage = this.page - 1;
		}
		if (this.previouspage < 0) {
			this.previouspage = 0;
		}
		return previouspage;
	}

	public void checkPage() {
		if (this.pagesize == 0) {
			/**
			 * 默认分页15条
			 */
			this.pagesize = 15;
		}
		this.pagetotal = this.total /this.pagesize;
		if (this.pagetotal * this.pagesize < this.total) {
			this.pagetotal = this.pagetotal + 1;
		}
		if (this.page > this.pagetotal) {
			this.page = this.pagetotal;
		}
		if (this.page <= 0) {
			/**
			 * 默认第一页
			 */
			if (this.pagetotal == 0) {
				this.page = 0;
			} else {
				this.page = 1;
			}
		}
		firstrownum = (this.page - 1) * this.pagesize;
		if (firstrownum < 0) {
			firstrownum = 0;
		}
		lastrownum = firstrownum + this.pagesize;
		if (lastrownum >= this.total) {
			lastrownum = this.total;
		}
	}

	/*
	 * rongrong.ding add
	 * 通用化查询数据用，具有通用化设置firstrownum和lastrownum方法的
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	public void getData(Object object,int rows){
		this.setTotal(rows);
		checkPage();
		if (rows != 0) {
			try {
				// 设置起始行数方法
				Method setfirstMethod = object.getClass().getMethod(
						this.setfirstrownummethod, int.class);
				// 设置最大行数方法
				Method setmaxMethod = object.getClass().getMethod(
						this.setmaxrownummethod, int.class);
				// 调用list()方法
				Method listMethod = object.getClass().getMethod(this.listmethod);
				//设置起始行
				object = setfirstMethod.invoke(object, this.firstrownum);
				//设置结束行
				object = setmaxMethod.invoke(object, this.lastrownum
						- this.firstrownum);
				//调用list()方法
				this.setDataList((List) listMethod.invoke(object));
			} catch (SecurityException e) {
				System.out.println( new Date()+" : "+e.toString());
			} catch (NoSuchMethodException e) {
			    System.out.println( new Date()+" : "+e.toString());
			} catch (IllegalArgumentException e) {
			    System.out.println( new Date()+" : "+e.toString());
			} catch (IllegalAccessException e) {
			    System.out.println( new Date()+" : "+e.toString());
			} catch (InvocationTargetException e) {
			    System.out.println( new Date()+" : "+e.toString());
			}
		}
	}

	public void setTotal(long total) {
		this.total = total;
		this.checkPage();
	}

	public void setPagetotal(int pagetotal) {
		this.pagetotal = pagetotal;
	}

	public void setNextpage(int nextpage) {
		this.nextpage = nextpage;
	}

	public void setPreviouspage(int previouspage) {
		this.previouspage = previouspage;
	}

	public void setLastpage(int lastpage) {
		this.lastpage = lastpage;
	}

	@SuppressWarnings("unchecked")
	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public long getFirstrownum() {
		return firstrownum;
	}

	public void setFirstrownum(int firstrownum) {
		this.firstrownum = firstrownum;
	}

	public long getLastrownum() {
		return lastrownum;
	}

	public void setLastrownum(int lastrownum) {
		this.lastrownum = lastrownum;
	}

	/**
	 * @return the msg
	 */
	public String getMsg(){
		if(this.message != null && !"".equals(this.message)){
			return this.message;
		}else{
			return null;
		}
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message){
		if(message != null){
			this.message = message;
		}else{
			this.message = "";
		}
	}

	public String getBack() {
		return back;
	}

	public void setBack(String back) {
		this.back = back;
	}

	public String getNowTime() {
		return nowTime;
	}

	public void setNowTime(String nowTime) {
		this.nowTime = nowTime;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

}
